import 'package:xianyu_school/utils/FixTabBarView.dart';
import 'package:xianyu_school/xianyuApp/Search/FSSearchPage.dart';
import 'package:flutter/material.dart';
import 'Header/fsHomeHedaer.dart';
import 'getTypeGoodsPage.dart';

class FSHomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _FSHomePageState();
  }
}

class _FSHomePageState extends State<FSHomePage> with  SingleTickerProviderStateMixin {
  //AutomaticKeepAliveClientMixin 切换tab后保留tab的状态
  List<String> _list = ["新鲜","手机","电脑","家具","书籍"];

  TabController _tabController;
  PageController _pageController;

//  @override
//  bool get wantKeepAlive => true;
  
  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: _list.length, vsync: this);
    _tabController.addListener( () {
      var index = _tabController.index;
      var previewIndex = _tabController.previousIndex;
      print('index:$index, preview:$previewIndex');
    });
    _pageController = PageController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: buildAppBarWidget(),
      body: buildBodyWidget(),
    );
  }

  Widget buildAppBarWidget() {
    return  AppBar(
      titleSpacing: 0,
      title: buildSearchBar(),
      centerTitle: false,
      elevation: 0,
      backgroundColor: Colors.yellow,  
      actions: [
        IconButton(
          splashColor: Colors.transparent,
          padding: EdgeInsets.all(0),
          icon: Icon(Icons.crop_free),
          onPressed: () {
          },
        ),
      ],
    );
  }

  Widget buildBodyWidget() {
    return Scrollbar(//Material风格的滚动条
      child: buildNestedScrollView(),
    );
  }

  Widget buildNestedScrollView() {
    return NestedScrollView(
      headerSliverBuilder: ( (BuildContext context, bool innerIsScrolled) {
        //头
        return [
          SliverToBoxAdapter(
            //在使用CustomScrollView创建自定义滚动效果的时候，CustomScrollView只能包含sliver系列组件，如果包含普通的组件,使用SliverToBoxAdapter包裹。
            child: FSHomeHedaer(),
          ),
          // bar 
          SliverOverlapAbsorber(
            handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
            sliver: SliverAppBar(
              pinned: true,
              titleSpacing: 0,
              forceElevated: false,
              automaticallyImplyLeading: false,
              backgroundColor: innerIsScrolled ? Colors.white : Colors.grey[100],
              title: TabBar(
                labelColor: Colors.black,
                controller: _tabController,
                indicatorColor: Colors.yellow,
                indicatorWeight: 3,
                indicatorPadding: EdgeInsets.fromLTRB(0, 0, 0, 6),
                unselectedLabelStyle: TextStyle(color: Colors.black45, fontSize: 16),
                labelStyle: TextStyle(color: Colors.black, fontSize: 18, fontWeight: FontWeight.bold),
                indicatorSize: TabBarIndicatorSize.label,
                isScrollable: true,
                tabs: _list.map( (String name) {
                  return Tab(text: name,);
                }).toList(),
                onTap: (index){
                  _pageController.jumpToPage(index);
                },
              ),
            ),
          ),
        ];
      }),

      body: FixTabBarView(
        pageController: _pageController,
        tabController: _tabController,
        children:[
          getTypeGoodsPage(t: 0,),
          getTypeGoodsPage(t: 1,),
          getTypeGoodsPage(t: 2,),
          getTypeGoodsPage(t: 3,),
          getTypeGoodsPage(t: 4,),
        ]
//        children: _list.map( (String name) {
//          return FHHomeGoodGrid(categoryType: name,);//获取数据
//        }).toList(),
      ),
    );
  }

  Widget buildListWidget(String name) {
    return SafeArea(
      //SafeArea通过MediaQuery检测屏幕的尺寸使应用程序的大小与屏幕适配。
      top: false,
      bottom: false,
      child: Builder(builder: (BuildContext context) {
        return CustomScrollView(
          key: PageStorageKey(name),
          slivers: <Widget>[
            SliverOverlapInjector(
              handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
            ),
            SliverPadding(
              padding: const EdgeInsets.only(bottom: 0),
              sliver: SliverFixedExtentList(
                itemExtent: 50,
                delegate: SliverChildBuilderDelegate( 
                  (BuildContext context, int index) {
                    return Text("index = $index");
                  },
                  childCount: 30
                ),
              ),
              
            ),
          ],
        );
      }),
    );
  }
  
  Widget buildSearchBar() {
    return Row(
      children: <Widget>[
        SizedBox(
          width: 10,
        ),

        Text(
          "闲鱼",
          style: TextStyle(
            color: Colors.black,
            fontSize: 20,
            fontWeight: FontWeight.bold,
          ),
        ),
     
        SizedBox(width: 10),
        Expanded(
          child: GestureDetector(
            onTap: () {
              Navigator.push(
                context,
                PageRouteBuilder( // 无动画的切换
                  pageBuilder: (context, anim1, anim2) => FSSearchPage(),
                ),
              );
            },
            child: Container(
              height: 30,
              padding: EdgeInsets.only(left: 10, right: 10),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(15)),
              ),
              child: Row(
                children: <Widget>[
                  Icon(
                    Icons.search,
                    size: 18,
                    color: Colors.grey[400],
                  ),

                  SizedBox(width: 5),
                  
                  Text(
                    "iPhoneXr手机壳",
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 14,
                      color: Colors.grey,
                      fontWeight: FontWeight.w300
                    )
                  )
                ],
              ),
            ),
          )
        ) 
      ],
    );
  }
}